
const Joi = require('joi');

module.exports = (schema)=> {
    return function (req, res, next) {
        let par = Object.assign(req.body, req.query, req.params);
        Joi.validate(par, schema, (err) => {
            if (err) {
                let details = err && err.details || [];
                let failures = [];
                for (let detail of details) {
                    failures.push(detail.message);
                }
                throw(JSON.stringify(failures))
            }
        });
        next()
    }
};